//
//  TJHelpController.m
//  Teleport
//
//  Created by botu　 on 2020/7/31.
//  Copyright © 2020 byx. All rights reserved.
//

#import "TJHelpController.h"
#import "TJStoreController.h"
#import "TJAdHomeController.h"
#import "TJNewController.h"
#import "TJQuestionController.h"
@interface TJHelpController ()<UIGestureRecognizerDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) UIView *maskView;
@property (weak, nonatomic) UIView *contentView;
@property (strong, nonatomic) NSMutableArray *datas;
@property (weak, nonatomic) UICollectionView *collectionView;
@property (assign, nonatomic) CGFloat leftMargin;
@end

@implementation TJHelpController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = UIColor.tj_alertMaskColor;
        self.contentView.tj_x = self.leftMargin;
    } completion:^(BOOL finished) {
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    _leftMargin = 160;
    
    UIView *maskView = [UIView tj_addToView:self.view];
    maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
    maskView.frame = self.view.bounds;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMaskView)];
    [maskView addGestureRecognizer:tap];
    tap.delegate = self;
    
    UIView *contentView = [UIView tj_addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.userInteractionEnabled = YES;
    [maskView addSubview:contentView];
    contentView.frame = CGRectMake(KScreenWidth, 0, KScreenWidth - _leftMargin, KScreenHeight);
    
    
    //关闭按钮
    UIButton *closeButton = [UIButton buttonWithSuperView:contentView font:UIFont_Px(12) color:UIColor_Hex(0x3781E7) title:@"".localizedString];
    [closeButton setImage:[UIImage imageNamed:@"buy_right"] forState:UIControlStateNormal];
    [closeButton setImage:[UIImage imageNamed:@"buy_right"] forState:UIControlStateHighlighted];
    
    [closeButton addTarget:self action:@selector(tapMaskView) forControlEvents:UIControlEventTouchUpInside];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(24);
        make.width.offset(24);
        make.left.offset(10);
        make.top.offset(16+StatusBarHeight);
    }];
    UILabel *helpLabel = [UILabel lableWithSuperView:contentView font:MediumFont(16) color:UIColor_Hex(0x111111) title:@"帮助中心".localizedString textAlignment:NSTextAlignmentCenter];
    
    [helpLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(closeButton.mas_bottom).offset(20);
        make.left.equalTo(closeButton);
    }];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.tableFooterView = [[UIView alloc] init];
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = 44;
    [tableView registerClass:UITableViewCell.self forCellReuseIdentifier:style1Cell];
    [contentView addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(helpLabel.mas_bottom).offset(10);
        make.left.right.bottom.equalTo(contentView);
        
    }];
    
    
    _contentView = contentView;
    _maskView = maskView;
    
    
    //跟新用户信息
    weakify(self);
    [TJNetworking.shareNetworking updateUserInfoAndLoading:NO success:^(TJUserInfo * _Nonnull userInfo) {
        if ([userInfo.storeState isEqualToString:@"PASS"]) {
            [weakself.datas addObject:@"发布广告".localizedString];
            [tableView reloadData];
        }
        
        
    }];
    
}



- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view != self.maskView) {
        return NO;
    }else {
        return YES;
    }
}


- (void)tapMaskView {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_x = KScreenWidth;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:style1Cell forIndexPath:indexPath];
    cell.textLabel.text = self.datas[indexPath.row];
    cell.textLabel.textColor = [UIColor darkGrayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:
             [self.navigationController pushViewController:[TJNewController new] animated:YES];
            break;
        case 1:
            [self.navigationController pushViewController:[TJQuestionController new] animated:YES];
            break;
        case 2:
        {
            
            [self storeApplicationClick];
            
        }
            
            break;
        case 3:
            [self.navigationController pushViewController:[TJAdHomeController new] animated:YES];
            break;
        default:
            break;
    }
    
}
/**商家申请 */
- (void)storeApplicationClick{
    //跟新用户信息
    weakify(self);
    [TJNetworking.shareNetworking updateUserInfoAndLoading:NO success:^(TJUserInfo * _Nonnull userInfo) {
        if (![userInfo.cardState isEqualToString:@"PASS"]) {
            [weakself alertWithTitle:@"请先实名认证".localizedString message:nil leftButtonName:@"确定".localizedString rightButtonName:nil leftButtonBlock:nil rightButtonBlock:nil];
            return;
        }
        
        if ([userInfo.storeState isEqualToString:@"WAIT"]) {
            [weakself alertWithTitle:@"认证中...".localizedString message:nil leftButtonName:@"确定".localizedString rightButtonName:nil leftButtonBlock:nil rightButtonBlock:nil];
            return;
        }
        if ([userInfo.storeState isEqualToString:@"PASS"]) {
            [weakself alertWithTitle:@"已认证".localizedString message:nil leftButtonName:@"确定".localizedString rightButtonName:nil leftButtonBlock:nil rightButtonBlock:nil];
            return;
        }
        
        [weakself.navigationController pushViewController:[TJStoreController new] animated:YES];
    }];
    
}
- (NSMutableArray *)datas {
    if (!_datas) {
        NSMutableArray *arr = @[@"新手引导".localizedString, @"常见问题".localizedString, @"商家申请".localizedString].mutableCopy;
        
        _datas = arr;
    }
    return _datas;
    
}
@end
